﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.UnitiveDocument.Approve
{
    /// <summary>
    /// Listview 的摘要说明。
    /// </summary>
    public partial class ListviewPage : Page
    {
        private void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (!Page.IsPostBack)
            {
                ViewState["SortField"] = "DocAddedDate";
                ViewState["SortDirect"] = "ASC";
                Bangding();
            }
        }

        public string GetRealNameStr(string Username)
        {
            if (Username != "")
            {
                return Components.Staff.GetRealNameByUsername(Username);
            }
            return "";
        }

#if false
        private void dgMyApprove_SelectedIndexChanged(object sender, EventArgs e)
        {}

        private void lbApproveDocument_Click(object sender, EventArgs e)
        {}

        private void lbThowAwayDocument_Click(object sender, EventArgs e)
        {}
#endif

        private string GetSelectedItemID(string controlID)
        {
            string selectedID = "";
            //遍历DataGrid获得checked的ID
            foreach (DataGridItem item in dgMyApprove.Items)
            {
                if (((CheckBox)item.FindControl(controlID)).Checked)
                {
                    selectedID += dgMyApprove.DataKeys[item.ItemIndex] + ",";
                }
            }
            if (selectedID.Length > 0)
            {
                selectedID = selectedID.Substring(0, selectedID.Length - 1);
            }
            return selectedID;
        }

        private void btnApproveDocument_Click(object sender, EventArgs e)
        {
            String strIDs = GetSelectedItemID("DocID");
            if (strIDs == "")
            {
                Response.Write("<script language='javascript'>window.alert('请选择要批阅的文档！');</script>");
            }
            else
            {
                var mySQL = new Database();
                string UserName = SiteContext.Current.UserName;

                SqlParameter[] parameters = {
                    Database.MakeInParam("@DocIDs", SqlDbType.NVarChar, 300, strIDs),
                    Database.MakeInParam("@Approver", SqlDbType.NVarChar, 300, UserName)
                };

                if (mySQL.RunProc("dbo.sproc_ApproveDocument", parameters) > 0)
                {
                    Response.Write("<script language='javascript'>window.alert('审批文件成功!');</script>");
                    Bangding();
                }
                else
                {
                    Response.Write("<script language='javascript'>window.alert('审批文件成功!');</script>");
                }
            }
        }

        private void btnThowAwayDocument_Click(object sender, EventArgs e)
        {
            String strIDs = GetSelectedItemID("DocID");
            if (strIDs == "")
            {
                Response.Write("<script language='javascript'>window.alert('请选择要丢弃的文档！');</script>");
            }
            else
            {
                var mySQL = new Database();

                SqlParameter[] parameters = {
                    Database.MakeInParam("@DocIDs", SqlDbType.NVarChar, 3000, strIDs),
                    Database.MakeInParam("@DeleteType", SqlDbType.Bit, 1, 0)
                };

                if (mySQL.RunProc("dbo.sproc_DeleteDocument", parameters) > 0)
                {
                    Response.Write("<script language='javascript'>window.alert('丢弃文件成功!');</script>");
                    Bangding();
                }
                else
                {
                    Response.Write("<script language='javascript'>window.alert('丢弃文件失败!');</script>");
                }
            }
        }

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dgMyApprove.CurrentPageIndex = e.NewPageIndex;
            Bangding();
        }

        #endregion

        #region 绑定DBGRID

        private void Bangding()
        {
            var mySQL = new Database();
            string UserName = SiteContext.Current.UserName;

            SqlParameter[] parameters = {
                Database.MakeInParam("@UserName", SqlDbType.NVarChar, 255, UserName),
                Database.MakeInParam("@RightCode", SqlDbType.Int, 4, 2)
            };

            DataTable dt;
            using (SqlDataReader dr = mySQL.RunDataReader("dbo.sproc_GetMyApproved", parameters))
            {
                dt = Tools.ConvertDataReaderToDataTable(dr);
            }
            //			//在DataTable的末尾加上空行，使得DataGrid固定行数
            //			int iBlankRows = dgMyApprove.PageSize - (dt.Rows.Count % dgMyApprove.PageSize);
            //			
            //			for (int i = 0; i < iBlankRows; i++)
            //			{
            //				dt.Rows.Add(dt.NewRow()) ;
            //			}

            dgMyApprove.DataSource = dt.DefaultView;
            dgMyApprove.DataBind();
            //对于空纪录不显示checkbox

            //			if(dgMyApprove.CurrentPageIndex  == dgMyApprove.PageCount -1 )
            //			{
            //				for(int i= (dgMyApprove.PageSize - iBlankRows)  ;i<dgMyApprove.Items.Count;i++)
            //				{
            //					dgMyApprove.Items[i].FindControl("DocID").Visible = false;
            //				}
            //			}

            btnThowAwayDocument.Attributes["onclick"] = "javascript:return confirm('您确认要丢弃这些文档吗?');";
            btnApproveDocument.Attributes["onclick"] = "javascript:return confirm('您确认要批阅这些文档吗?');";
        }

        #endregion

        #region 将数据排序

        /// <summary>
        /// 将信件按照指定字段进行排序
        /// </summary>	
        public void DataGrid_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            if (ViewState["SortField"].ToString() == e.SortExpression)
            {
                ViewState["SortDirect"] = (ViewState["SortDirect"].ToString() == "ASC") ? "DESC" : "ASC";
            }
            else
            {
                ViewState["SortField"] = e.SortExpression;
                ViewState["SortDirect"] = "ASC";
            }

            foreach (DataGridColumn col in dgMyApprove.Columns)
            {
                if (col.SortExpression == ViewState["SortField"].ToString())
                {
                    //					if(ViewState["SortDirect"].ToString() == "ASC")
                    //					{						
                    //						col.HeaderText +="<img src='../../../images/asc.gif' border=0 />";						
                    //					}
                    //					else
                    //						col.HeaderText +="<img src='../../../images/desc.gif' border=0 />";
                }
            }

            Bangding();
        }

        #endregion

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN：该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.btnApproveDocument.Click += new System.EventHandler(this.btnApproveDocument_Click);
            this.btnThowAwayDocument.Click += new System.EventHandler(this.btnThowAwayDocument_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion
    }
}